var dialogArr = new Array();
var dialogIndex = 0;
var TALK_WINDOW_MIN_WIDTH=520;
var TALK_WINDOW_MIN_HEIGHT=484;
var MIDDLE_WIN_WIDTH=7;
var DEFAULT_RIGHT_WINDOW_WIDTH=150;
var OTHER_RIGHT_WINDOW_WIDTH=240;
var MSG_SEND_AREA_HEIGHT=150;
function openDialog(srcObj, src, title, width, height, isIframe) {
	var isExist=false;
	for ( var i = 0; i < dialogArr.length; i++) {
		//prevObject是为了获取打开dialog的对象
		if(dialogArr[i]!=null&&(dialogArr[i]['prevObject'].attr('id')=="Dialog-"+srcObj.attr('id'))){
			isExist=true;
			var dialogObj = dialogArr[i];
			if (!dialogObj.is(":visible")) {
				dialogObj.find("#minDialog").attr("state", "");
				dialogArr[i].show();
			}else{
				dialogArr[i]['prevObject'].dialog("moveToTop");//跳转至最上层
			}
			break;
		}
	}
	if(isExist){
		return;
	}
	var dialogHtml = "";
	var diaObj = null;
	if (isIframe=='iframe') {
		dialogHtml = "<div id='Dialog-" + srcObj.attr("id") + "' dialogIndex="+dialogIndex+"><iframe src='" + src
				+ "' style='border:0px;width:100%;height:100%;'/></div>";
		diaObj = $(dialogHtml);
		diaObj.appendTo("body");
	} else if(isIframe=='talkWindow') {
		dialogHtml = $("#talkWindow").html();
		diaObj = $(dialogHtml);
		diaObj.appendTo("body").attr("id","Dialog-"+srcObj.attr("id")).attr("dialogIndex",dialogIndex);
	}else{
		dialogHtml = srcObj.html();
		diaObj = srcObj;
	}
	diaObj.dialog(
					{
						title : title,
						width : width == null ? 600 : width,
						height : height == null ? 400 : height,
						autoOpen : false,
						close:function(){
							diaObj.dialog("destroy");
							if($dialog.attr('id')!='IMBar'){
								diaObj.parent().remove();
								diaObj.remove();
							}
						},
						open : function(event, ui) {
//							zoomIn($(this).parent(),$(this).width(),$(this).height());
//							zoomIn($(this).parent(),$(this).parent().width(),$(this).parent().width());
							var $dialog = $(this);
							var currIndex = dialogIndex;
							$dialog.css({
								"overflow" : "hidden",
								'padding' : 0,
								'margin' : 0
							});
							var resizeHtml = '<a role="button" id="closeDialog" class="ui-dialog-titlebar-close ui-corner-all" href="#"><span class="ui-icon ui-icon-closethick"></span></a>';
							resizeHtml += '<a role="button" id="maxDialog" state="" class="ui-dialog-titlebar-close ui-corner-all" style="margin-right:16px;" href="#"><span class="ui-icon ui-icon-extlink"></span></a>';
							resizeHtml += '<a role="button" id="minDialog" state="" class="ui-dialog-titlebar-close ui-corner-all" style="margin-right:32px;" href="#"><span class="ui-icon ui-icon-minusthick"></span></a>';
							if(isIframe=='talkWindow'){//如果是聊天窗口，则去掉对话框的顶部
								diaObj.parent().find("div.ui-dialog-titlebar").find(".ui-dialog-title").html("");
								diaObj.find(".topWin").appendTo(diaObj.parent().find(".ui-dialog-title"));
							}
							$dialog.parent().find(".ui-dialog-titlebar-close").replaceWith(resizeHtml);
							titleBarBtnEvent($dialog.parent(),srcObj,$dialog,isIframe,currIndex);
						}
						
						
					}).dialog("open").parent().unbind('click').bind('click',function(){//绑定事件，当点击对话框时清除对应的task的定时任务
						var taskID=$(this).find(".ui-dialog-content").attr("dialogindex");
						clearTaskBarInterval(taskID);
					});
	dialogArr[dialogIndex] = diaObj.parent();
	srcObj.attr("dialogIndex", dialogIndex);
	$(".taskContainer").prepend(
			"<div class='task' id='" + dialogIndex + "' state=''>" + title
					+ "</div>");
	//任务数超过时缩小task的宽度
	var taskNum=$(".taskContainer").find(".task").length;
	var totalNum=parseInt($(window).width()/114);
	if(taskNum>totalNum){
		$(".task").css({
			width:$(window).width()/taskNum-2//-2是因为宽度不是整除可能导致异常
		});
	}
	$(".task").unbind('hover').bind('hover', function() {
		$(this).toggleClass("task_hover");
	}, function() {
		$(this).toggleClass("task_hover");
	}).unbind('click')
			.bind(
					'click',
					function() {
						var index = $(this).attr("id");
						clearTaskBarInterval(index);
						var dialogObj = dialogArr[index];
						if (!dialogObj.is(":visible")) {// 如果是隐藏的窗口，则显示在最上层
							dialogObj.find("#minDialog").attr("state", "");
							var maxIndex = 0;
							for ( var i = 0; i < dialogArr.length; i++) {
								if (dialogArr[i] != null) {
									var zIndex = dialogArr[i].css("z-index");
									maxIndex = (maxIndex <= zIndex ? zIndex
											: maxIndex);
									dialogArr[i].css("z-index", dialogArr[i]
											.css("z-index") - 1);
								}
							}
							dialogObj.css({
								"z-index" : maxIndex
							});
							dialogObj.show();
						} else {// 如果未隐藏，则直接显示在最上层
							var maxIndex = 0;
							for ( var i = 0; i < dialogArr.length; i++) {
								if (dialogArr[i] != null) {
									var zIndex = dialogArr[i].css("z-index");
									maxIndex = (maxIndex <= zIndex ? zIndex
											: maxIndex);
									if (i != index) {
										dialogArr[i]
												.css("z-index", dialogArr[i]
														.css("z-index") - 1);
									}
								}
							}
							if (maxIndex == dialogObj.css("z-index")) {// 判断当前对话框是否在最上层，如果为最上层，则直接隐藏
								dialogObj.hide();
								saveLastState(dialogObj.find(".ui-dialog-content"));
								return;
							}// 否则，显示在最上层
							dialogObj.css({
								"z-index" : maxIndex
							});
							dialogObj.show();
						}
					});
	dialogIndex++;
}


function titleBarBtnEvent(dialogObj,srcObj,dialog,isIframe,currIndex){
	dialogObj.find(".ui-dialog-titlebar-close").hover(function() {
		$(this).toggleClass("ui-state-hover");
	}, function() {
		$(this).toggleClass("ui-state-hover");
	});
	dialogObj.find("#closeDialog").unbind('click').bind('click',function() {
		dialogArr[currIndex] = null;
		srcObj.attr("dialogIndex", "");
		$(".taskContainer").find(
				"#" + currIndex).remove();
		dialog.dialog("destroy");
		if(dialog.attr('id')!='IMBar'){
			dialog.remove();
		}
	});
	dialogObj.find("#maxDialog").unbind('click').bind('click',function() {
			var wid = $(window).width();
			var hei = $(window).height();
			if ($(this).attr("state") == '') {
				saveLastState(dialog);
				dialog.dialog("option", "position",['left','top']);
				dialog.dialog("option", "height",hei-65);
				dialog.dialog("option", "width",wid);
				if(isIframe=='talkWindow'){
					talkWindowResize(dialog);
				}
				$(this).attr("state","max").find("span").removeClass('ui-icon-extlink').addClass("ui-icon-newwin");
			} else {
				showLastState(dialog);
				if(isIframe=='talkWindow'){
					talkWindowResize(dialog);
				}
				$(this).attr("state", "").find("span").removeClass('ui-icon-newwin').addClass("ui-icon-extlink");
			}

		});
	dialogObj.find("#minDialog").unbind('click').bind('click', function() {
		if ($(this).attr("state") == '') {
			saveLastState(dialog);
			dialogObj.hide();
			$(this).attr("state", "min");
		} else {
			showLastState(dialog);
			if(isIframe=='talkWindow'){
				talkWindowResize(dialog);
			}
			$(this).attr("state", "");
		}
	});
}
/**
 * 保存操作之前的窗口对象的位置和大小信息
 * @param obj 当前弹窗对象
 */
function saveLastState(obj){
	var pObj=obj.parent();
	pObj.attr("lastLeft",pObj.css("left"));
	pObj.attr("lastTop",pObj.css("top"));
	pObj.attr("lastWidth",obj.dialog("option", "width"));
	pObj.attr("lastHeight",obj.dialog("option", "height"));
}
/**当最大化和最小化窗口还原时，操作此方法显示上次的完整状态
 * @param obj 当前弹窗对象
 */
function showLastState(obj){
	var pObj=obj.parent();
	pObj.css({
		left : pObj.attr('lastLeft'),
		top : pObj.attr('lastTop'),
	});
	obj.dialog("option", "height",pObj.attr("lastHeight"));
	obj.dialog("option", "width",pObj.attr("lastWidth"));
}

/**
 * 聊天窗口缩放调整代码
 * @param openedDia 当前聊天窗口
 */
function talkWindowResize(openedDia){
	var dialogWid=openedDia.parent().css("width").replace("px","");
	var dialogHei=openedDia.parent().css("height").replace("px","");
	if(dialogWid<=TALK_WINDOW_MIN_WIDTH){
		openedDia.dialog("option","width",TALK_WINDOW_MIN_WIDTH);//限制最小宽度
	}
	if(dialogHei<=TALK_WINDOW_MIN_HEIGHT){
		openedDia.dialog("option","height",TALK_WINDOW_MIN_HEIGHT);//限制最小高度
	}
	var rightWidth=openedDia.find(".rightWin").attr("type")=='default'?DEFAULT_RIGHT_WINDOW_WIDTH:OTHER_RIGHT_WINDOW_WIDTH;
	if(openedDia.find(".middleWin").attr('state')!='open'){
		openedDia.find(".msgShowArea").css({
			height:openedDia.height()-MSG_SEND_AREA_HEIGHT								
		});
		openedDia.find(".leftWin").css({
			width:openedDia.width()-MIDDLE_WIN_WIDTH
		});
	}else{
		openedDia.find(".msgShowArea").css({
			height:openedDia.height()-MSG_SEND_AREA_HEIGHT
		});
		openedDia.find(".leftWin").css({
			width:openedDia.width()-MIDDLE_WIN_WIDTH-rightWidth
		});
	}
	openedDia.find(".middleWin").css({
		height:openedDia.height()
	});
	openedDia.find(".rightWin").css({
		height:openedDia.height()
	}).children().css({
		height:openedDia.height()
	});
}